package com.glodon.paas.document.web.controller;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.FormParam;
import javax.ws.rs.core.MediaType;

import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.glodon.paas.account.api.bean.User;
import com.glodon.paas.consts.StringConst;
import com.glodon.paas.document.api.bean.File;
import com.glodon.paas.document.api.bean.Revision;
import com.glodon.paas.document.service.FileService;
import com.glodon.paas.document.service.RecyclerService;
import com.glodon.paas.document.service.util.OrderUtilsFile;
import com.glodon.paas.document.service.util.WebService;
import com.glodon.paas.document.web.util.SecurityUtils;
import com.glodon.paas.exception.ExistException;
import com.glodon.paas.exception.MergePartsException;
import com.glodon.paas.exception.NotFoundException;
import com.glodon.paas.exception.PaasServerException;
import com.glodon.paas.exception.PaasServerExceptionFactory;
import com.glodon.paas.exception.PrivilegeException;
import com.glodon.paas.exception.i18n.Message;
import com.glodon.paas.storage.BulkStorage;
import com.glodon.paas.storage.ReadResult;
import com.glodon.paas.util.BeanFactoryUtil;
import com.glodon.paas.util.FileUtil;

@Controller
@RequestMapping("/file")
public class FileController {

    public static final Logger     LOGGER = LoggerFactory.getLogger(FileController.class);

    @Autowired
    private FileService        fileService;

    @Autowired
    private RecyclerService    recyclerService;
    
    @Autowired
    private ActivityPublisher  activityPublisher;
    
    @Autowired
    private WebService webService;

    public void setFileService(FileService service) {
        fileService = service;
    }

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public ModelAndView viewListPage(ModelMap model) {
        model.addAttribute(StringConst.USED_SPACE, fileService.getUsedSpace(SecurityUtils.getCurrentUser().getId()));
        return new ModelAndView("document", model);
    }

    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    public ModelAndView viewDetailPage(@FormParam("id")
    String id, ModelMap model) {
        File file = fileService.getFile(SecurityUtils.getCurrentUser().getId(), id, true);
        // 填充版本
        List<Revision> revisions = fileService.getRevisions(SecurityUtils.getCurrentUser().getId(), file.getId());
        file.setRevisions(revisions);

        model.addAttribute(StringConst.FILE, file);
        
        // Activity
        if (file != null) {
            //activityPublisher.view(user.getId(), user.getDisplayName(), file.getId(), file.getName());
        }
        return new ModelAndView("detail");
    }
    
    @RequestMapping(value = "", method = RequestMethod.GET)
    public void getRootFiles(@RequestParam(value = "navigation", required = false)String navigation,
                             @RequestParam(value = "personal", required = false)String personal,
                          @RequestParam(value = "project", required = false)String project, // project id
                          @RequestParam(value = "type", required = false)Integer type, // 文件类型
                          @RequestParam(value = "orderBy", required = false, defaultValue = "updateTime")String orderBy, 
                          @RequestParam(value = "orderType", required = false)String orderType, 
                          @RequestParam(value = "startIndex", defaultValue = "0")Integer startIndex, 
                          @RequestParam(value = "count", defaultValue = "10000")Integer count, 
                          ModelMap model) {
        String userId = SecurityUtils.getCurrentUser().getId();
        List<File> files = new ArrayList<File>();
        //个人文件信息查询
        if(personal != null){
            files.addAll(fileService.getFiles(userId, userId, orderBy, orderType, true));
        }
        // 项目文件信息查询
        if(project != null ){
            User user = SecurityUtils.getCurrentUser();
            List<com.glodon.paas.account.api.bean.Project> accountProjects = webService.getUser(user.getId()).getProjects();
            Set<String> ids = new HashSet<String>();
            for (com.glodon.paas.account.api.bean.Project p : accountProjects) {
                ids.add(p.getId());
            }
            for(String id : ids){
                try{
                    files.add(fileService.getProjectFolder(user.getId(), id));
                }catch (PrivilegeException e) {
                    LOGGER.warn("User["+user.getId()+"] can not get Project["+id+"]");
                }
                catch (NotFoundException e) {
                    LOGGER.warn(e.getMessage());
                }
            }
            OrderUtilsFile.order(files, orderBy, orderType);
        }
        model.addAttribute(StringConst.ITEMS, files);
        // 导航条信息
        if(navigation != null){
            model.addAttribute(StringConst.PATH, StringConst.SEPARATOR_SLASH);
        }
    }

    @RequestMapping(value = "/{fileId}", method = RequestMethod.GET)
    public void getFiles( @PathVariable(value = "fileId") String fileId, 
                          @RequestParam(value = "path", required = false)String path, // 获取文件导航条
                          @RequestParam(value = "child", required = false)String child, // 获取子文件信息
                          @RequestParam(value = "tree", required = false)String tree, // 获取该文件到根目录的树结构
                          @RequestParam(value = "orderBy", required = false, defaultValue = "updateTime")String orderBy, 
                          @RequestParam(value = "orderType", required = false)String orderType, 
                          @RequestParam(value = "startIndex", defaultValue = "0")Integer startIndex, 
                          @RequestParam(value = "count", defaultValue = "10000")Integer count, 
                          ModelMap model) {
        String userId = SecurityUtils.getCurrentUser().getId();
        List<File> items = new ArrayList<File>();
        if(child != null){
            items.addAll(fileService.getFiles(userId, fileId, orderBy, orderType, true));
        }
        else if(tree != null){
            items.add(fileService.getTree(userId, fileId));
        }
        else{
            items.add(fileService.getFile(userId, fileId, true));
        }
        model.addAttribute(StringConst.ITEMS, items);
        // 导航条信息
        if (path != null) {
            model.addAttribute(StringConst.PATH, fileService.getPath(userId, fileId));
        }
    }
    
    @RequestMapping(value = "/usedSpace", method = RequestMethod.GET)
    public void getUsedSpace(ModelMap model) {
        model.addAttribute("space", fileService.getUsedSpace(SecurityUtils.getCurrentUser().getId()));
    }

//    @RequestMapping(value = "/recycle", method = RequestMethod.GET)
//    public void getRecycle(@RequestParam(value = "projectId", required = false)
//    String projectId, @RequestParam(value = "orderBy", required = false, defaultValue = "updateTime")
//    String orderBy, @RequestParam(value = "orderType", required = false)
//    String orderType, ModelMap model) {
//        String ownerId = SecurityUtils.getCurrentUser().getId();
//        // modified for project
//        if (!StringUtils.isEmpty(projectId)) {
//            ownerId = projectId;
//        }
//        List<File> files = recyclerService.getFiles(ownerId);
//        OrderUtilsFile.order(files, orderBy, orderType);
//        model.addAttribute(StringConst.ITEMS, files);
//    }
    
    @RequestMapping(value = "", method = RequestMethod.POST)
    @ResponseBody
    public Object createFolder(@RequestBody File file, HttpServletRequest request,  ModelMap model) {
        String userId = SecurityUtils.getCurrentUser().getId();
        if(StringUtils.isBlank(file.getParentId())){
            file.setParentId(userId);
        }
        return fileService.createFolder(userId, file.getParentId(), fileService.generateFolderName(userId, file.getParentId()));
    }

    @RequestMapping(value = "", method = RequestMethod.PUT)
    @ResponseBody
    public Object updateFile(@RequestParam("id") String id, 
                             @RequestParam(value="operate", required=false)String operate,
                             @RequestParam(value="name", required=false)String name,
                             @RequestParam(value="parentId", required=false)String parentId,
                             ModelMap model){
        if(StringConst.RENAME.equalsIgnoreCase(operate)){
            return rename(id, name);
        }
        if(StringConst.MOVE.equalsIgnoreCase(operate)){
            this.move(id, parentId, model);
            return model;
        }
        return null;
    }
    
//    @RequestMapping(value = "", method = RequestMethod.PUT)
//    @ResponseBody
    public Object rename(@RequestParam("id") String id,  @RequestParam("name")String name) {
        User currentUser = SecurityUtils.getCurrentUser();
        File oldFile = fileService.getFile(currentUser.getId(), id, false);
        File newFile = fileService.rename(currentUser.getId(), id, name);
        // Activity
        activityPublisher.rename(currentUser.getId(), currentUser.getDisplayName(), id, oldFile.getName(), name, true);
        return newFile;
    }

//    @RequestMapping(value = "", method = RequestMethod.PUT)
    public void move(@RequestParam("id") String id, @RequestParam("parentId")String parentId, ModelMap model) {
         User user = SecurityUtils.getCurrentUser();
         List<File> successItems = new ArrayList<File>();
         if (StringUtils.isNotEmpty(id)) {
            String[] fileIds = id.split(StringConst.SEPARATOR_COMMA);
            List<String> failedItems = new ArrayList<String>();
            for (String fileId : fileIds) {
                try {
                    successItems.add(fileService.moveFile(user.getId(), fileId, parentId));
                } catch (ExistException ee) {
                    failedItems.add(fileService.getPath(user.getId(), fileId, false));
                } catch (Exception e) {
                    failedItems.add(fileId);
                }
            }
            if (!failedItems.isEmpty()) {
                model.addAttribute(StringConst.FAILED_ITEMS, failedItems);
            }
            model.addAttribute(StringConst.SUCCESS_ITEMS, successItems);
        }
        
        // Activity
        activityPublisher.moveFile(user.getId(), user.getDisplayName(),
                                   successItems, parentId, true);
    }

    @RequestMapping(value = "/{fileId}", method = RequestMethod.DELETE)
    public void delete(@PathVariable("fileId")String fileId,
                       ModelMap model) throws PaasServerException {
        User user = SecurityUtils.getCurrentUser();
        List<File> files = new ArrayList<File>();
        if (StringUtils.isNotEmpty(fileId)) {
            String[] fileIds = fileId.split(StringConst.SEPARATOR_COMMA);
            List<String> failedItems = new ArrayList<String>();
            List<String> successItems = new ArrayList<String>();
            for (String id : fileIds) {
                try {
                    files.add(fileService.remove(user.getId(), id));
                    successItems.add(id);
                } catch (Exception e) {
                    failedItems.add(id);
                }
            }
            if (!failedItems.isEmpty()) {
                model.addAttribute(StringConst.FAILED_ITEMS, failedItems);
            }
            model.addAttribute(StringConst.SUCCESS_ITEMS, successItems);
            model.addAttribute(StringConst.RESULT, StringConst.SUCCESS);
        }

        // Activity
        activityPublisher.logicalDelFile(user.getId(), user.getDisplayName(), files, true);
    }

//    @RequestMapping(value = "/delete", method = RequestMethod.POST)
//    public void delFile(@FormParam("id") String id, ModelMap model) {
//        User user = SecurityUtils.getCurrentUser();
//        if (StringUtils.isNotEmpty(id)) {
//            String[] fileIds = id.split(StringConst.SEPARATOR_COMMA);
//            List<String> failedItems = new ArrayList<String>();
//            for (String fileId : fileIds) {
//                try {
//                    recyclerService.remove(user.getId(), fileId);
//                } catch (Exception e) {
//                    failedItems.add(fileId);
//                }
//            }
//            if (!failedItems.isEmpty()) {
//                model.addAttribute(StringConst.FAILED_ITEMS, failedItems);
//            }
//            model.addAttribute(StringConst.RESULT, StringConst.SUCCESS);
//        }
//        
//        // Activity ???
//        //activityPublisher.delFile(user.getId(), user.getDisplayName(), fileIds, fileNames);
//    }

//    @RequestMapping(value = "/deleteRevision", method = RequestMethod.POST)
//    public void delReversion(@FormParam("id") String id, @FormParam("revision") String revision, ModelMap model) {
//        User user = SecurityUtils.getCurrentUser();
//        File file = fileService.getFile(user.getId(), id, false);
//        
//        if (StringUtils.isNumeric(revision)) {
//            fileService.removeRevision(user.getId(), id, Integer.valueOf(revision));
//            model.addAttribute(StringConst.RESULT, StringConst.SUCCESS);
//        } else {
//            throw PaasServerExceptionFactory.validateException("Revision Num is incorrect.", null);
//        }
//        
//
//        // Activity
//        activityPublisher.delReversion(user.getId(), user.getDisplayName(), file.getId(), file.getName(), true);
//    }

//    @RequestMapping(value = "/cleanRecycler", method = RequestMethod.GET)
//    public void cleanRecycler(ModelMap model) {
//        recyclerService.clean(SecurityUtils.getCurrentUser().getId());
//        model.addAttribute(StringConst.RESULT, StringConst.SUCCESS);
//    }

//    @RequestMapping(value = "/restore", method = RequestMethod.POST)
//    public void restoreFile(@FormParam("id") String id, ModelMap model) {
//        User user = SecurityUtils.getCurrentUser();
//        List<String> failedItems = new ArrayList<String>();
//        List<String> errorItems = new ArrayList<String>();
//        List<String> successItems = new ArrayList<String>();
//        List<String> resultItems;
//        if (StringUtils.isNotEmpty(id)) {
//            String[] fileIds = id.split(StringConst.SEPARATOR_COMMA);
//            for (String fileId : fileIds) {
//                resultItems = recyclerService.restore(user.getId(), fileId);
//                if (!resultItems.isEmpty()) {
//                    failedItems.addAll(resultItems);
//                    errorItems.add(fileId);
//                }else{
//                    successItems.add(fileId);
//                }
//            }
//            if (!failedItems.isEmpty()) {
//                model.addAttribute(StringConst.FAILED_ITEMS, failedItems);
//                model.addAttribute(StringConst.ERROR_ITEMS, errorItems);
//            }
//            model.addAttribute(StringConst.RESULT, StringConst.SUCCESS);
//        }
//        
//     // Activity
//     
//     activityPublisher.restoreFile(user.getId(), user.getDisplayName(), successItems, true);
//    }

    @RequestMapping(value = "/getUploadedBytes", method = RequestMethod.GET)
    public void getUploadedBytes(@FormParam("parentId")String parentId, 
                                 @FormParam("name")String name, 
                                 @FormParam("size")String size, 
                                 ModelMap model) {
        String userId = SecurityUtils.getCurrentUser().getId();
        if (StringUtils.isBlank(parentId)) {
            parentId = userId;
        }
        String[] names = name.split(StringConst.SEPARATOR_QUOTATION);
        String[] sizes = size.split(StringConst.SEPARATOR_COMMA);
        if (names.length != sizes.length) {
            throw PaasServerExceptionFactory.validateException("[names count] and [sizes count] are incorrect.", null);
        }
        Map<String, Long> result = new HashMap<String, Long>();
        for (int i = 0; i < names.length; i++) {
            String n = names[i];
            long s = Long.valueOf(sizes[i]);
            // 块大小，默认为5m,配合 document使用
            if (s < BulkStorage.BLOCK_SIZE) {
                result.put(String.valueOf(i), 0L);
                continue;
            } else {
                Revision v = fileService.getUploadingRevision(userId, parentId, n, s);
                if (v == null) {
                    result.put(String.valueOf(i), 0L);
                } else {
                    long uploadedBytes = fileService.getUploadedBytes(v.getFileId());
                    // remove old uploading revision
                    if (uploadedBytes > s) {
                        fileService.removeUploadingRevision(userId, v.getFileId());
                    }
                    // mergechunk
                    else if (uploadedBytes == s) {
                        try {
                            fileService.uploadFile(userId, parentId, name, s, s, null);
                            // merge success
                            result.put(String.valueOf(i), Long.valueOf(size));
                        } catch (MergePartsException e) {
                            // merge fail
                            fileService.removeUploadingRevision(userId, v.getFileId());
                            result.put(String.valueOf(i), 0L);
                        }
                    } else {
                        result.put(String.valueOf(i), uploadedBytes);
                    }
                }
            }
        }
        model.addAttribute(StringConst.RESULT, result);
    }
    @RequestMapping(value="upload", method = RequestMethod.POST)
    public void uploadFile(HttpServletRequest request, ModelMap model) {
        InputStream stream = null;
        try {
            User user = SecurityUtils.getCurrentUser();
            String parentId = null;
            String fullName = null;
            String startPosition = "0";
//            String projectId = null;
            File file = null;
            long size = 0;
            DiskFileItemFactory df = new DiskFileItemFactory();
            ServletFileUpload upload = new ServletFileUpload(df);
            FileItemIterator iter = upload.getItemIterator(request);
            while (iter.hasNext()) {
                FileItemStream item = iter.next();
                String name = item.getFieldName();
                stream = item.openStream();
                if (item.isFormField()) {
                    String value = IOUtils.toString(stream, "UTF-8");
                    if (("parentId").equals(name)) {
                        parentId = value;
                    } else if ("size".equals(name) && StringUtils.isNumeric(value)) {
                        size = Long.valueOf(value);
                    } else if ("fullName".equals(name)) {
                        fullName = value;
                    } else if ("position".equals(name)) {
                        startPosition = value;
                    }
                } else {
                    // ownerId and parnetId is not empty
                    if(StringUtils.isBlank(parentId)){
                        parentId = user.getId();
                    }

                    // web上传 默认采用 断点续传
                    file = fileService.uploadFile(user.getId(), parentId, fullName, Long.valueOf(startPosition), size, stream);
                    model.addAttribute(StringConst.ITEMS, file);
                }
            }

            model.addAttribute(StringConst.RESULT, StringConst.SUCCESS);
            
            // Activity
            if (file != null) {
                activityPublisher.uploadFile(user.getId(), user.getDisplayName(), file.getId(), file.getName(), true);
            }
        } catch (PaasServerException de) {
            String text = de.getMessage();
            Message message = (Message) BeanFactoryUtil.getBean("message");
            if (message != null) {
                text = message.getText(de);
            }
            model.addAttribute("message", text);
            model.addAttribute(StringConst.RESULT, StringConst.FAIL);
            LOGGER.error(de.getMessage(), de);
        } catch (Exception e) {
            throw PaasServerExceptionFactory.systemException(e.getMessage(), e);
        } finally {
            if (stream != null) {
                try {
                    stream.close();
                } catch (IOException ignore) {
                }
            }
        }
    }

    @RequestMapping(value = "/download", method = RequestMethod.GET)
    public void downloadFile(@FormParam("id") String id, @FormParam("revision") String revision, HttpServletRequest request, HttpServletResponse response, ModelMap model) {
        User user = SecurityUtils.getCurrentUser();
        // check if the request contains Range header, and extract the range
        // values if existing
        long start = -1, end = -1;
        String rangeHeader = request.getHeader("Range");
        boolean doRange = false;
        if (rangeHeader != null) {
            String startStr = StringUtils.substringBetween(rangeHeader, "bytes=", "-");
            String endStr = StringUtils.substringAfter(rangeHeader, "-");
            if (StringUtils.isNumeric(startStr) && StringUtils.isNumeric(endStr)) {
                start = Long.parseLong(startStr);
                if (StringUtils.isNotBlank(endStr)) {
                    end = Long.parseLong(endStr);
                }
                doRange = true;
            }
        }

        int fileRevision = -1;
        if (StringUtils.isNumeric(revision)) {
            fileRevision = Integer.parseInt(revision);
        }

        InputStream is = null;
        OutputStream os = null;

        try {
            ReadResult streamResponse = null;
            response.reset();
            response.setCharacterEncoding(StringConst.UTF8);
            if (doRange) {
                streamResponse = fileService.downloadFile(user.getId(), id, fileRevision, start, end);
                response.setHeader("Accept-Ranges", "bytes");
                response.setHeader("Content-Range",
                                   "bytes " + streamResponse.getRanges()[0] + "-" + streamResponse.getRanges()[1] + "/"
                                           + streamResponse.getRanges()[2]);
                response.setStatus(206);
            } else {
                streamResponse = fileService.downloadFile(user.getId(), id, fileRevision);
            }

            is = streamResponse.getInputStream();
            os = response.getOutputStream();
            response.addHeader("Content-Disposition",
                               "attachment; filename=\"" + FileUtil.encodeFileName(request, streamResponse.getName())
                                       + "\"");
            response.addHeader("Content-Length", Long.toString(streamResponse.getLength()));
            response.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            IOUtils.copy(is, os);


            // Activity 动态
            activityPublisher.downloadFile(user.getId(), user.getDisplayName(), id,streamResponse.getName(),true);
        } catch (PaasServerException de) {
            throw de;
        } catch (Exception er) {
            throw PaasServerExceptionFactory.systemException(er.getMessage(), er);
        } finally {
            IOUtils.closeQuietly(is);
            IOUtils.closeQuietly(os);
        }
    }

    @RequestMapping(value = "/package", method = RequestMethod.GET)
    public void downloadPackage(@FormParam("id") String id, HttpServletRequest request, HttpServletResponse response, ModelMap model) {
        User user = SecurityUtils.getCurrentUser(); 
        try {
            String[] ids = id.split(StringConst.SEPARATOR_COMMA);
            if (ids.length > 1) {
                downloadFiles(user.getId(), ids, request, response);
            } else {
                File folder = fileService.getFile(user.getId(), id, false);
                response.addHeader("Content-Disposition",
                                   "attachment; filename=\""
                                           + FileUtil.encodeFileName(request, folder.getName() + ".zip") + "\"");
                response.setContentType(MediaType.APPLICATION_OCTET_STREAM);
                fileService.downloadFolder(user.getId(), id, response.getOutputStream());
                
                // Activity 动态
                activityPublisher.downloadFile(user.getId(), user.getDisplayName(), id,folder.getName(), true);

            }
        } catch (PaasServerException de) {
            throw de;
        } catch (Exception er) {
            throw PaasServerExceptionFactory.systemException(er.getMessage(), er);
        }
    }

    private void downloadFiles(String userId, String[] ids, HttpServletRequest request, HttpServletResponse response) {
        User currentUser = SecurityUtils.getCurrentUser();
        
        try {
            String fileName = (String) request.getParameter("name");
            if (StringUtils.isEmpty(fileName)) {
                fileName = "广联达文档";
                if (ids.length > 0) {
                    File file = fileService.getFile(userId, ids[0], false);
                    if (userId.equals(file.getCreator())) {
                        fileName = "我的文档";
                        if (!userId.equals(file.getParentId())) {
                            fileName = fileService.getFile(userId, file.getParentId(), false).getFullName();
                        }
                    } else {
                        fileName = "他人共享";
                        try {
                            fileName = fileService.getFile(userId, file.getParentId(), false).getFullName();
                        } catch (Exception ignore) {
                        }
                    }
                }
            }
            response.addHeader("Content-Disposition",
                               "attachment; filename=\"" + FileUtil.encodeFileName(request, fileName + ".zip") + "\"");
            response.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            fileService.downloadFiles(userId, ids, response.getOutputStream());
            
            // Activity
            activityPublisher.downloadFiles(currentUser.getId(), currentUser.getDisplayName(),
                                            ids, null, true);
        } catch (PaasServerException de) {
            throw de;
        } catch (Exception er) {
            throw PaasServerExceptionFactory.systemException(er.getMessage(), er);
        }
    }

    @RequestMapping(value = "/lock", method = RequestMethod.POST)
    public void lockFile(@RequestParam("id")
    String id, ModelMap model) {
        String userId = SecurityUtils.getCurrentUser().getId();
        fileService.lockFile(userId, id);
        model.addAttribute(StringConst.RESULT, StringConst.SUCCESS);
    }

    @RequestMapping(value = "/unlock", method = RequestMethod.POST)
    public void unlockFile(@RequestParam("id")
    String id, ModelMap model) {
        String userId = SecurityUtils.getCurrentUser().getId();
        fileService.unlockFile(userId, id);
        model.addAttribute(StringConst.RESULT, StringConst.SUCCESS);
    }
}
